package org.personal.mybatis.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import lombok.Getter;
import lombok.Setter;
import org.apache.ibatis.annotations.Mapper;

import java.io.FileInputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class Main {
    public static void main(String[] args) {
        Config config = new Config();

        // 使用 FastAutoGenerator 快速配置代码生成器
        FastAutoGenerator.create(config.getDataSourceUrl(), config.getDataSourceUserName(), config.getDatasourcePassword())
                .globalConfig(builder -> {
                    builder.author(config.getAuthor()) // 设置作者
                            .outputDir(getOuterDir()) // 输出目录
                            .disableOpenDir();
                })
                .packageConfig(builder -> {
                    builder.parent(config.getParentPackageName()) // 设置父包名
                            .entity(config.getEntityPackageName()) // 设置实体类包名
                            .mapper(config.getMapperPackageName()) // 设置 Mapper 接口包名
                            .service(config.getServicePackageName()) // 设置 Service 接口包名
                            .serviceImpl(config.getServiceImplPackageName()) // 设置 Service 实现类包名
                            .xml(config.getXmlPackageName()); // 设置 Mapper XML 文件包名
                })
                .strategyConfig(builder -> {
                    builder.addInclude(config.getTableNames()) // 设置需要生成的表名
                            .addTablePrefix(config.getTableFilterPrefix())
                            .entityBuilder()
                            .enableLombok() // 启用 Lombok
                            .enableTableFieldAnnotation() // 启用字段注解

                            .controllerBuilder()
                            .disable() // 不生成controller

                            .mapperBuilder()
                            .enableBaseResultMap() // 生成mapper中的resultMap
                            .enableInsert()
                            .enableInsertBatch()
                            .enableUpdateBatch()
                            .enableServiceAutowireMapper()
                            .mapperAnnotation(Mapper.class)
                            .build();
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用 Freemarker 模板引擎
                .execute(); // 执行生成

    }

    public static String getOuterDir() {
        String userDir = System.getProperty("user.dir");
        return userDir + "/src/main/java";
    }

    public static TemplateConfig mapperTemplate() {
        return new TemplateConfig.Builder()
                .xml("templates/mapper.xml.vm")
                .build();
    }


    @Setter
    @Getter
    public static class Config {
        private String parentPackageName;
        private String entityPackageName;
        private String mapperPackageName;
        private String servicePackageName;
        private String serviceImplPackageName;
        private String xmlPackageName;

        private String dataSourceUrl;
        private String dataSourceUserName;
        private String datasourcePassword;

        private String author;

        private List<String> tableNames;
        private List<String> tableFilterPrefix;

        public Config() {
            Properties properties = new Properties();
            try {
                FileInputStream fileInputStream = new FileInputStream(System.getProperty("user.dir") + "/src/main/resources/config.properties");
                properties.load(fileInputStream);
                fileInputStream.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            parentPackageName = properties.getProperty("parent.package.name");
            entityPackageName = properties.getProperty("entity.package.name");
            mapperPackageName = properties.getProperty("mapper.package.name");
            servicePackageName = properties.getProperty("service.package.name");
            serviceImplPackageName = properties.getProperty("serviceImpl.package.name");
            xmlPackageName = properties.getProperty("xml.package.name");

            dataSourceUrl = properties.getProperty("datasource.url");
            dataSourceUserName = properties.getProperty("datasource.username");
            datasourcePassword = properties.getProperty("datasource.password");

            author = properties.getProperty("author");

            tableNames = Arrays.stream(properties.getProperty("table.names").split(",")).map(String::trim).toList();
            tableFilterPrefix = Arrays.stream(properties.getProperty("table.filter.prefixs").split(",")).map(String::trim).toList();

        }
    }
}